<?php

namespace api\web\controller;


use api\web\model\NewsModel;
use cmf\controller\RestBaseController;


class NewsController extends RestBaseController
{
    public function getNewsList()
    {
        $keyword = input('get.keyword','');
        $where = [];
        if ($keyword) {
            $where[] = ['title|author', 'like', '%' . $keyword . '%'];
        }

        //页码
        $page = input('page/d',1);
        //每页返回的条数
        $pageSize = input('page_size/d',10);
        $list = NewsModel::where($where)
            ->order('list_order','desc')
            ->field('id,title,image,create_time,abstract')
            ->page($page,$pageSize)
            ->select();

        $this->success('新闻公告表', $list);
    }

    public function getNewsInfo()
    {
        $id = input('id/d');
        if (empty($id)) {
            $this->error('查询失败，ID不能为空');
        }

        $news = NewsModel::withoutField('delete_time,list_order')->find($id);
        if (!$news) {
            $this->error('查询失败，新闻不存在');
        }

        // 解码 HTML 实体并替换文件 URL
        $content = htmlspecialchars_decode($news['content']);
        $content = cmf_replace_content_file_url($content, true);

        // 移除所有 HTML 标签
        $content = strip_tags($content);

        $news['content'] = $content;
        $this->success('新闻公告表', $news);
    }
}